package com.stm.meet.Controller;

import com.stm.meet.Form.User;
import com.stm.meet.Utils.Response;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    Logger logger = LoggerFactory.getLogger(AdminController.class);


    @Autowired
    Response response;

    @Autowired
    JedisPool jedisPool;

    @RequestMapping(value = {"login",""})
    public String login(){
        return "admin/login";
    }

    @RequestMapping("main")
    public String main(){
        return "admin/main";
    }

    @RequestMapping("welcome")
    public String weicome(){
        return "admin/welcome";
    }

    @RequestMapping(value = "logindo",method = RequestMethod.POST)
    @ResponseBody
    public Response loginDo(@RequestBody User user, HttpServletRequest request)  {
        Jedis jedis = jedisPool.getResource();
        String code =  jedis.get(request.getRequestedSessionId()+"imagecode");

        if (!user.getVerify().equals(code)){
            response.setCode(0);
        }else if (!user.getUsername().equals("admin") || !user.getPassword().equals("admin")){
            request.getSession().setAttribute("login","admin");
            response.setCode(200);
        }else{
            response.setCode(-1);
        }
        return response;
    }

}
